<script setup lang="ts">
import { PropType, ref } from 'vue'
import type { TableData } from '@/api/uc/org/types'
import { Descriptions } from '@/components/Descriptions'
import { useI18n } from '@/hooks/web/useI18n'
import { ElTag, ElImage } from 'element-plus'

// 获取字典
import { useCache } from '@/hooks/web/useCache'
import { ContentWrap } from '@/components/ContentWrap'
import { spDrugInOutOrderSeeApi } from '@/api/cy/DrugGoTo'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}
const { t } = useI18n()
const prop = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<TableData>>,
    default: () => null
  },
  detailSchema: {
    type: Array as PropType<DescriptionsSchema[]>,
    default: () => []
  },
  id: {
    type: String,
    default: null
  }
})
console.log(prop)

const tableData = ref<any>([])

const tableData1 = ref<any>([])

// (修改) 调用查看接口获取信息
const getMedicalOrderSee = async () => {
  const res = await spDrugInOutOrderSeeApi({ id: prop.id })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    if (res.data.type == '1') {
      tableData1.value = await res.data.detailed
    } else if (res.data.type == '2') {
      tableData.value = await res.data.detailed

      if (tableData.value.length > 0) {
        console.log('1')
        for (let i = 0; i < tableData.value.length; i++) {
          let num = ref(0)
          if (tableData.value[i].batch) {
            for (let index = 0; index < tableData.value[i].batch.length; index++) {
              console.log(tableData.value[i].batch[index].actual_number, '000')
              num.value += Number(tableData.value[i].batch[index].actual_number)
            }
          }

          console.log(num.value)
          tableData.value[i].actual_number = num.value
        }
      }
    }

    await tableData.value
    await tableData1.value
    // console.log(await tableData.value, '222')
  } else {
    tableData.value = []
    tableData1.value = []
    // await methods.setValues(data.value)
  }
}
const init = async () => {
  await getMedicalOrderSee()
}
init()
</script>

<template>
  <div>
    <Descriptions :schema="prop.detailSchema" :data="prop.currentRow || {}">
      <template #is_stop="{ row }: { row: TableData }">
        <ElTag :type="row.is_stop === '0' ? 'success' : row.is_stop === '1' ? 'warning' : 'danger'">
          {{
            row.is_stop === '0' ? '启用' : row.is_stop === '1' ? '停用' : t('tableDemo.commonly')
          }}
        </ElTag>
      </template>

      <template #org_type="{ row }: { row: TableData }">
        <div>{{ getDictLabel('org_type', row.org_type) }}</div>
      </template>
      <template #state="{ row }: { row: TableData }">
        <div>{{ getDictLabel('forage_out_in_state', row.state) }}</div>
      </template>

      <template #type="{ row }: { row: any }">
        <div>{{ getDictLabel('in_out_type', row.type) }}</div>
      </template>
      <template #out_type="{ row }: { row: any }">
        <div>{{ getDictLabel('Drug_out_type', row.out_type) }}</div>
      </template>
      <template #in_type="{ row }: { row: any }">
        <div>{{ getDictLabel('Drug_in_type', row.in_type) }}</div>
      </template>
      <template #business_license="{ row }: { row: TableData }">
        <ElImage v-if="row.business_license" :src="row.business_license" />
      </template>

      <template #identity_card_front="{ row }: { row: TableData }">
        <ElImage v-if="row.identity_card_front" :src="row.identity_card_front" />
      </template>

      <template #identity_card_back="{ row }: { row: TableData }">
        <ElImage v-if="row.identity_card_back" :src="row.identity_card_back" />
      </template>
    </Descriptions>
    <ContentWrap title="出库明细" v-if="prop.currentRow.type == '2'">
      <el-table :data="tableData" :border="false" style="width: 100%" :default-expand-all="true">
        <el-table-column type="expand">
          <template #default="props">
            <div m="4">
              <el-table :data="props.row.batch" :border="false">
                <el-table-column label="批次明细" prop="name" />
                <el-table-column label="批次号" prop="batch_code" />

                <el-table-column label="出库数量" prop="actual_number" />
                <!-- <el-table-column label="Address" prop="address" />
            <el-table-column label="Zip" prop="zip" /> -->
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="药品" prop="drug_name" />
        <el-table-column label="申请数量" prop="apply_number" />
        <el-table-column label="实际总数量" prop="actual_number" />
      </el-table>
    </ContentWrap>

    <ContentWrap title="入库明细" v-if="prop.currentRow.type == '1'">
      <el-table :data="tableData1" :border="true" style="width: 100%" :default-expand-all="true">
        <el-table-column type="expand">
          <template #default="props">
            <div m="4">
              <el-table :data="props.row.batch" :border="true">
                <el-table-column label="批次明细" prop="name" width="80" />
                <el-table-column label="批次号" prop="batch_code" />

                <el-table-column label="生产日期" prop="start_date" />

                <el-table-column label="过期日期" prop="end_date" />

                <el-table-column label="实际数量" prop="actual_number" />
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="药品" prop="drug_name" />
        <el-table-column label="金额" prop="amount" />
        <el-table-column label="初始数量" prop="apply_number" />
        <el-table-column label="实际数量" prop="total_actual_number" />
      </el-table>
    </ContentWrap>
  </div>
</template>
